//To contain the class

#include "ShowFindUpdate.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>

using namespace std;

ShowFindUpdate::ShowFindUpdate() //default constructor
{
}

/*Archie debugging sites*/

// Function : 1b. print all menu items sorted by Chef [print item name, ID and Price]
// Type	: Main Function
// PIC : Archie
// Status : Complete
void ShowFindUpdate::PrintMenuItemChef(vector<Menu_item> TheMIVector, vector<Recipe> TheRecipeVector)
{

	//Comparator object to sort the vector object --- by Chef
	struct ChefComparationClass {
		bool operator() (Recipe i,Recipe j) { return (i.getChefName().compare(j.getChefName()) < 0) ;}
	} ChefComparator;

    //this seems to be breaking
	//sort (TheRecipeVector.begin(), TheRecipeVector.end(), ChefComparator); //Sort the Recipe Vector by Chef Name

	for(int i = 0; i < TheRecipeVector.size();i++)
	{
		//get the data
		int RecipeTemp = TheRecipeVector[i].getRecipeId(); //get recipe id
		string RecipeNameTemp = MenuItemRec_IdToName(TheMIVector,RecipeTemp);
		double RecipePriceTemp = MenuItemRec_IdToPrice(TheMIVector,RecipeTemp);
		//print the data
		cout << TheRecipeVector[i].getChefName() << " " << RecipeNameTemp << " price : " << RecipePriceTemp << " " << "\n";
	}
	/*//For debugging
	//print the vector
	for(int i = 0; i < TheRecipeVector.size();i++)
	{
		TheRecipeVector[i].display();
	}

	cout <<"\n\n\n\nAfter that \n";


	sort (TheRecipeVector.begin(), TheRecipeVector.end(), ChefComparator);

	for(int i = 0; i < TheRecipeVector.size();i++)
	{
		TheRecipeVector[i].display();
	}
	*/
}

// Function : In Menu Item, from RecipeId, return the Recipe Name
// Type	: Support Function
// PIC : Archie
// Status : Complete
string ShowFindUpdate::MenuItemRec_IdToName(vector<Menu_item> TheMIVector, int ComparedRecipeData)
{
	for(int i = 0; i < TheMIVector.size();i++)
	{
		if (TheMIVector[i].getRecipeId() ==ComparedRecipeData) //if equal, return the Name
		{
			return TheMIVector[i].getMenuName();
		}
	}
	return ""; //if no equal, return blank string
}

// Function : In Menu Item, from RecipeId, return the Recipe Price
// Type	: Support Function
// PIC : Archie
// Status : Complete
double ShowFindUpdate::MenuItemRec_IdToPrice(vector<Menu_item> TheMIVector, int ComparedRecipeData)
{
	for(int i = 0; i < TheMIVector.size();i++)
	{
		if (TheMIVector[i].getRecipeId() ==ComparedRecipeData) //if equal, return the Name
		{
			return TheMIVector[i].getprice();
		}
	}
	return 0; //if no equal, return zero
}

void ShowFindUpdate::PrintMenuItemPrice(vector<Menu_item> mi_vect) //B1c print all menu items sorted by Price TTL
{
    cout << endl;
	//Menu_item::Menu_item temp();
	//Arrange the vector
	for(int i = 0; i<mi_vect.size();i++)
	{
	    for(int j = 0; j<mi_vect.size()-(i+1);j++)
	    {
	        if(mi_vect[j].Menu_item::get_price()<mi_vect[j+1].Menu_item::get_price())
	        {
	            Menu_item::Menu_item temp(mi_vect[j+1]);
	            mi_vect[j+1].Menu_item::copy(mi_vect[j]);
	            mi_vect[j].Menu_item::copy(temp);
	        }
	    }
	}

	//print the vector
	for(int i = 0; i < mi_vect.size();i++)
	{
		cout << "\nItem Name: " << mi_vect[i].Menu_item::get_menu_item_name() << endl;
		cout << "Price: " << mi_vect[i].Menu_item::get_price() << endl;
		cout << "Description: " << mi_vect[i].Menu_item::get_descr_string() << endl;
	}

}

void ShowFindUpdate::FindMenuItemPrice(int itemID, const vector<Menu_item>& menu_items, const vector<Order_Item> order_items)
{
	double item_price = -1;
	int order_total = 0;
	string item_name = "";
	for(int i = 0; i<menu_items.size(); i++)
	{
		if(itemID == menu_items[i].get_menu_item_id())
		{
			item_price = menu_items[i].get_price();
			item_name = menu_items[i].get_menu_item_name();
		}
	}
	if(item_price = -1)
	{
		cout << "\n\nERROR: Invalid Menu ID.\n\n";
		return;
	}
	
	for(int i = 0; i<order_items.size(); i++)
	{
		if(itemID == order_items[i].get_menu_item_id())
		{
			order_total += order_items[i].get_prod_qty();
		}
	}
	
	cout << "\nThe total sales for Menu Item ID " << item ID << " called " << item_name << " (priced at $" << setprecision(2) << item_price << ") is:";
	cout << "\n\tQuantity Ordered: " << order_total;
	cout << "\n\tTotal Sales: $" << order_total * (double)item_price << endl;
	
}
